using System;
using System.Collections;
using System.Collections.Generic;
using Castle.ActiveRecord;
using Castle.Components.Validator;
using NHibernate.Expression;
using Saffron.Framework.Users;
namespace Saffron.Framework.Projects
{
    [ActiveRecord]
    public class Task: ActiveRecordBase
    {
        private int _id;
        private string _name;
        private string _description;
        private DateTime _createdDate;
        private IList _taskOwners = new  List<UserAccount>();
        private IList _taskMembers = new  List<UserAccount>();
        private Project _project;

        [PrimaryKey]
        public int Id
        {
            get { return _id; }
            set { _id = value; }
        }

        [Property(NotNull = true, Unique = true), ValidateNonEmpty("Name is a required field")]
        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }

        [Property, ValidateNonEmpty("Description is a required field")]
        public string Description
        {
            get { return _description; }
            set { _description = value; }
        }
       
        [BelongsTo("TaskProject", NotNull = true)]
        public Project TaskProject
        {
            get { return _project; }
            set { _project = value; }
        }

        [Property]
        public DateTime CreatedDate
        {
            get { return _createdDate; }
            set { _createdDate = value; }
        }


        [HasAndBelongsToMany(typeof(UserAccount), Table = "TaskOwners", ColumnKey = "TaskId", ColumnRef = "UserAccountId")]
        public IList TaskOwners
        {
            get { return _taskOwners; }
            set { _taskOwners = value; }
        }

        [HasAndBelongsToMany(typeof(UserAccount), Table = "TaskMembers", ColumnKey = "TaskId", ColumnRef = "UserAccountId")]
        public IList TaskMembers
        {
            get { return _taskMembers; }
            set { _taskMembers = value; }
        }
        
        #region Static Methods
        public static Task[] GetTasksByProject(Project project)
        {
           return (Task[]) FindAllByProperty(typeof(Task), "TaskProject", project);
        }

        public static Task FindByPrimaryKey(int id)
        {
            return (Task)FindByPrimaryKey(typeof(Task), id);
        }
        #endregion 

        
         
        

    }
}
